package service

import (
	"fmt"
	"go-graduation/pb/router"
	"go-graduation/service/internal/data"
)

func NewArticleComment() *comment {
	return &comment{}
}

type comment struct {
}

// AddArticleCommentToMQ 添加评论数据到消息队列
func (a *comment) AddArticleCommentToMQ(userId int, content string) error {
	if userId == 0 {
		return fmt.Errorf("请先登录")
	}

	if content == "" {
		return fmt.Errorf("评论内容为空")
	}

	// todo 加入到消息队列

	return nil
}

// GetArticleCommentByPage 获取分页数据
func (a *comment) GetArticleCommentByPage(page int) ([]*router.ArticleCommentListResponse_Lists, error) {
	pageSize := 15
	pageStart := (page - 1) * pageSize
	rows, err := data.NewArticleComment().GetListByPage(pageStart, pageSize)
	if err != nil {
		return nil, err
	}

	var ids []int
	for _, row := range rows {
		ids = append(ids, row.ID)
	}

	if err != nil {
		return nil, fmt.Errorf("读取数据错误")
	}

	list := make([]*router.ArticleCommentListResponse_Lists, 0)
	for _, item := range rows {
		list = append(list, &router.ArticleCommentListResponse_Lists{
			Id:      int64(item.ID),
			UserId:  int64(item.UserId),
			Content: item.Content,
		})
	}

	return list, nil
}
